﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressAdminBusinessLayer;
using UserObj = AlexandriaPressAdminObject.UserInfo;
using MailSender;
using AlexandriaPressUserBusinessLayer;


namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm16 : System.Web.UI.Page
    {

        protected void Page_Init(object sender, EventArgs e)
        {                         
            if (HttpContext.Current.User.Identity.Name != "")
            {
                UserBusinsessLayer business = new UserBusinsessLayer();
                string _UserType = business.getUserType(HttpContext.Current.User.Identity.Name);
                if (_UserType != "Type_Admin")
                {
                    Response.Redirect("~/MasterPages/ErrorPage.aspx");
                }
            }                 
            
          
            
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                view(false, false);
            }
            lblCheck.Visible = false;
            lblDOS.Visible = false;
            
          
        }

        private void view(bool calendar, bool comment)
        {
            Label1.Visible = calendar;
            Calendar1.Visible = calendar;
            Label2.Visible = comment;
            TextBox3.Visible = comment;
            btnBan.Visible = false;
            btnSuspend.Visible = false;
        
        }
        private void BindData()
        {
            gridUserList.DataSource = UserBL.viewUserList();
            gridUserList.EmptyDataText = "No data available";
            gridUserList.DataBind();
        }

        protected void btnBan_Click(object sender, EventArgs e)
        {
            int countChecked = 0;
            int countAll;
            List<UserObj> user = new List<UserObj>();
            UserObj userObj;
            for (int i = 0; i < gridUserList.Rows.Count; i++)
            {
                CheckBox selected = (CheckBox)gridUserList.Rows[i].FindControl("cbSelect");
                if (selected.Checked)
                {
                    userObj = new UserObj();

                    userObj.ID = Convert.ToInt32(gridUserList.DataKeys[i]["ID"].ToString());
                    userObj.Email = Convert.ToString(gridUserList.DataKeys[i]["Email"].ToString());
                    userObj.Reason = TextBox3.Text;
                    EmailNotification.AdminMail("admin@alexandria.com", userObj.Email, null, null, "Ban notice", "You are banned effective today, " + DateTime.Now.ToString() + "due to the ff reasons:" + userObj.Reason);
                    
                    user.Add(userObj);
                    countChecked++;
                }
            }

            if (countChecked > 0)
            {
                countAll = UserBL.BanUser(user);
                BindData();
                EraseInput();
                lblCheck.Text = countAll + " user/s banned";
                lblCheck.Visible = true;
            }
            else 
            
            {
                lblCheck.Text = "Please select the user/s you want to ban";
                lblCheck.Visible = true;
            }
        }

        protected void btnSuspend_Click(object sender, EventArgs e)
        {
            int countAll;
            int countChecked = 0;
            if(Calendar1.SelectedDate != null){
            DateTime endDate = Calendar1.SelectedDate;

            if (endDate > DateTime.Now)
            {
                List<UserObj> user = new List<UserObj>();
                UserObj userObj;
                for (int i = 0; i < gridUserList.Rows.Count; i++)
                {
                    CheckBox selected = (CheckBox)gridUserList.Rows[i].FindControl("cbSelect");
                    if (selected.Checked)
                    {
                        userObj = new UserObj();
                        userObj.ID = Convert.ToInt32(gridUserList.DataKeys[i]["ID"].ToString());
                        userObj.Email = Convert.ToString(gridUserList.DataKeys[i]["Email"].ToString());
                        userObj.Reason = TextBox3.Text;
                        userObj.EndOfSuspension = endDate;
                        EmailNotification.AdminMail("admin@alexandria.com", userObj.Email, null, null, "Suspension notice", "You are suspended effective today, " + DateTime.Now.ToString() + " until " + userObj.EndOfSuspension + ", due to the ff reasons:" + userObj.Reason);
                        user.Add(userObj);
                        countChecked++;
                    }
                }

                if (countChecked++ > 0)
                {
                    countAll = UserBL.SuspendUser(user);
                    BindData();
                    EraseInput();
                    lblCheck.Text = countAll + " user/s suspended";
                    lblCheck.Visible = true;
                }
                else
                {
                    lblCheck.Text = "Please select the user/s you want to suspend";
                    lblCheck.Visible = true;
                }
            }
            else
            {
                lblDOS.Visible = true;
            }
 
        }
        }

        private void EraseInput()
        {
   
            TextBox3.Text = null;
            lblDOS.Visible = false;
            DropDownList4.SelectedIndex = 0;
        }

        public void gridUserList_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridUserList.Visible = true;
           gridUserList.PageIndex = e.NewPageIndex;
           gridUserList.DataSource = UserBL.viewUserList();
           gridUserList.DataBind();
        }

        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {
            if (TextBox3.Text != null)
            {
                if (DropDownList4.SelectedIndex == 1)
                {
                    btnBan.Visible = true;
                    btnSuspend.Visible = false;
                }
                else if (DropDownList4.SelectedIndex == 2)
                {
                        btnSuspend.Visible = true;
                        btnBan.Visible = false;
                }
                else
                {
                    btnBan.Visible = false;
                    btnSuspend.Visible = false;
                }
            }
            else
            {
                btnBan.Visible = false;
                btnSuspend.Visible = false;
            }

        }

        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList4.SelectedIndex == 1)
            {
                view(false, true);
            }
            else if (DropDownList4.SelectedIndex == 2)
            {
                view(true, true);
            }
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            DateTime date = Calendar1.SelectedDate;
            if (date < DateTime.Now)
            {
                lblDOS.Visible = true;
            }
        }
    }
}
